﻿//************************************************************************//
//文件功能：操作文件
//创 建 人：薄帅
//创建时间：2011年05月26日
//修 改 人：
//修改时间：
//************************************************************************//

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ProjBackEnd.Common.Helper
{
    public class OperateFile
    {
        /// <summary>
        /// 文件转换为二进制流
        /// </summary>
        /// <param name="filename">转换的文件名</param>
        /// <returns>byte[]</returns>
        public static byte[] GetBytes(string filename)
        {
            FileInfo fi = new FileInfo(filename);
            FileStream fs = fi.OpenRead();
            byte[] bytes = new byte[fs.Length];
            fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
            fs.Close();
            return bytes;
        }

        /// <summary>
        /// 复制目录
        /// </summary>
        /// <param name="source">源目录</param>
        /// <param name="destination">目的目录</param>
        public static void CopyDirection(string source, string destination)
        {
            if (!Directory.Exists(destination))
            {
                Directory.CreateDirectory(destination);
            }
            DirectoryInfo rootdir = new DirectoryInfo(source);

            //遍历文件
            FileInfo[] fileinfo = rootdir.GetFiles();
            foreach (FileInfo file in fileinfo)
            {
                file.CopyTo(destination + "\\" + file.Name, true);
            }

            //遍历子目录，递归调用以copy子目录
            DirectoryInfo[] childdir = rootdir.GetDirectories();
            foreach (DirectoryInfo dir in childdir)
            {
                CopyDirection(dir.FullName, destination + "\\" + dir.Name);
            }
        }
    }
}
